﻿using System;
using System.Collections.Generic;
using Framework.Dependency;
using Interfaces.Criteria;
using Interfaces.Filters;
using Interfaces.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using YQLDataProvider;
using YQLDataProvider.Providers;

namespace DataProviderIntegrationTests.ProvidersTests
{
    [TestClass]
    public class YqlIndustryDataProviderTests
    {
        [ClassInitialize]
        public static void InitTests(TestContext context)
        {
            var module = new YqlDataProviderModule(DependencyManager.Container);
            module.Initialize();
        }
        
        [TestMethod]
        public void GetDataTest_IsLazyLoadingTrue_OnlyIndustryDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given:(keyCriteria, filter) =>
                {
                    keyCriteria.Setup(x => x.IsLazyLoading).Returns(true);
                    keyCriteria.Setup(x => x.IsFullLoading).Returns(false);
                    keyCriteria.Setup(x => x.Identificators).Returns(new object[] { 112, 113 });
                    filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y); 
                },when: (provider,keyCriteria,filter) => provider.GetData(keyCriteria, filter), then: (result) =>
                    {
                        Assert.IsNotNull(result);
                        foreach (var industry in result)
                        {
                            Assert.IsNotNull(industry.Id);
                            Assert.IsNotNull(industry.Name);
                            Assert.IsNotNull(industry.Key);
                            Assert.IsNull(industry.Companies);
                            Assert.IsNull(industry.Sector);
                        }
                    });
        }

        [TestMethod]
        public void GetDataTest_IsLazyAndFullFalse_PartDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (keyCriteria, filter) =>
            {
                keyCriteria.Setup(x => x.IsLazyLoading).Returns(false);
                keyCriteria.Setup(x => x.IsFullLoading).Returns(false);
                keyCriteria.Setup(x => x.Identificators).Returns(new object[] { 112 });
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            }, when: (provider, keyCriteria, filter) => provider.GetData(keyCriteria, filter), then: (result) =>
            {
                Assert.IsNotNull(result);
                foreach (var industry in result)
                {
                    Assert.IsNotNull(industry.Id);
                    Assert.IsNotNull(industry.Name);
                    Assert.IsNotNull(industry.Key);
                    Assert.IsNotNull(industry.Companies);
                    Assert.IsNull(industry.Sector);
                }
            });
        }

        [TestMethod]
        public void GetDataTest_IsFullLoadingTrue_FullDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (keyCriteria, filter) =>
            {
                keyCriteria.Setup(x => x.IsLazyLoading).Returns(false);
                keyCriteria.Setup(x => x.IsFullLoading).Returns(true);
                keyCriteria.Setup(x => x.Identificators).Returns(new object[] { 112 });
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            }, when: (provider, keyCriteria, filter) => provider.GetData(keyCriteria, filter), then: (result) =>
            {
                Assert.IsNotNull(result);
                foreach (var industry in result)
                {
                    Assert.IsNotNull(industry.Id);
                    Assert.IsNotNull(industry.Name);
                    Assert.IsNotNull(industry.Key);
                    Assert.IsNotNull(industry.Companies);
                    foreach (var company in industry.Companies)
                    {
                        Assert.IsNotNull(company);
                        Assert.IsNotNull(company.Quoteis);
                        Assert.IsNotNull(company.Industry);
                    }
                    Assert.IsNull(industry.Sector);
                }
            });
        }


        private static void YqlIndustryProviderTetsTemplate(Action<Mock<IKeySupportedCriteria>, Mock<IIndustryFilter>> given,
                                                     Func<YqlIndustryDataProvider, IKeySupportedCriteria, IIndustryFilter, IEnumerable<IIndustry>> when,
                                                     Action<IEnumerable<IIndustry>> then)
        {
            var provider = new YqlIndustryDataProvider();
            var keyCriteria = new Mock<IKeySupportedCriteria>();
            var filter = new Mock<IIndustryFilter>();
            given(keyCriteria, filter);
            var result = when(provider, keyCriteria.Object, filter.Object);
            then(result);
        }
    }
}
